package chapter6.meine.exercises;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Timer;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



	public class RollingTwoDicesPanel extends JPanel implements ActionListener {
		private JButton rollButt;
		public  int diceVal1;
		public  int diceVal2;
		public Timer timer;

		public RollingTwoDicesPanel() {
			setBackground(Color.LIGHT_GRAY); // Set up appearance of the panel
			setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			setLayout(new BorderLayout());
			DicesPanel dices = new DicesPanel();
			dices.setPreferredSize( new Dimension(100,100) );
			add(dices, BorderLayout.CENTER);

			rollButt = new JButton("Roll");
			rollButt.addActionListener( new ActionListener() {
			      public void actionPerformed(ActionEvent evt) {
			          roll();
			       }
			    });
			add(rollButt, BorderLayout.SOUTH);
		}// end of constructor RollingTwoDicesPanel()
		
		public void roll(){
			 if (timer != null)
			      return;
			  timer = new Timer(100, new ActionListener() {
			      int frames = 1;
			      public void actionPerformed(ActionEvent evt) {
			    	  diceVal1 = (int)(Math.random()*6) + 1;
			    	  diceVal2 = (int)(Math.random()*6) + 1;
			         repaint();
			         frames++;
			         if (frames == 10) {
			          
						 timer.stop();
			            timer = null;
			         }
			      }
			   });
			   timer.start();
			}

		public class DicesPanel extends JPanel implements MouseListener {


			public DicesPanel() {
				setBackground(Color.LIGHT_GRAY); // Set up appearance of the
													// panel
				setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
				diceVal1 = (int) (Math.random() * 6) + 1;
				diceVal2 = (int) (Math.random() * 6) + 1;
				addMouseListener(this);
			}

			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				drawDice(g, diceVal1, 10, 10);
				drawDice(g, diceVal2, 60, 60);
			}

			public void drawDice(Graphics g, int diceVal, int x, int y) {
				g.setColor(Color.white);
				g.fillRect(x + 1, y + 1, 38, 38);
				g.setColor(Color.black);
				g.drawRect(x, y, 40, 40);
				switch (diceVal) {
				case 1:
					g.fillOval(x + 20, y + 20, 9, 9);
					break;
				case 2:
					g.fillOval(x + 3, y + 3, 9, 9);
					g.fillOval(x + 28, y + 28, 9, 9);

					break;
				case 3:
					g.fillOval(x + 3, y + 3, 9, 9);
					g.fillOval(x + 16, y + 16, 9, 9);
					g.fillOval(x + 28, y + 28, 9, 9);
					break;
				case 4:
					g.fillOval(x + 3, y + 3, 9, 9);
					g.fillOval(x + 28, y + 28, 9, 9);
					g.fillOval(x + 3, y + 28, 9, 9);
					g.fillOval(x + 28, y + 3, 9, 9);
					break;
				case 5:
					g.fillOval(x + 3, y + 3, 9, 9);
					g.fillOval(x + 28, y + 28, 9, 9);
					g.fillOval(x + 3, y + 28, 9, 9);
					g.fillOval(x + 28, y + 3, 9, 9);
					g.fillOval(x + 16, y + 16, 9, 9);

					break;
				case 6:
					g.fillOval(x + 3, y + 3, 9, 9);
					g.fillOval(x + 28, y + 28, 9, 9);
					g.fillOval(x + 3, y + 28, 9, 9);
					g.fillOval(x + 28, y + 3, 9, 9);
					g.fillOval(x + 3, y + 16, 9, 9);
					g.fillOval(x + 28, y + 16, 9, 9);
					break;
				default:
					break;
				}
			}// end of drawDice()

			public void mouseClicked(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}

		}// end of class DicesPanel

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
}// end of class RollingTwoDicesPanel

